package com.liuyk.commonlib.utility;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * 权限检查
 * 注意：如果需要多个权限的时候，尽量一个一个请求，等回调回来，在请求下一个
 * 以往测试发现有些机型一次请求多个权限会被覆盖，导致只会出现一个权限弹窗
 * <p>
 * Created by liuyakui on 2020/1/20.
 */
public final class PermissionUtils {
    public static final int PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1001;
    public static final int PERMISSIONS_READ_EXTERNAL_STORAGE = 1002;
    public static final int PERMISSIONS_READ_PHONE_STATE = 1003;
    public static final int PERMISSIONS_CAMERA = 1004;
    public static final int PERMISSIONS_CONTACTS = 1005;
    public static final int PERMISSIONS_LOCATION = 1006;

    public static void requestPermission(Activity activity, Permission permission, int requestCode) {
        requestPermission(activity, permission, requestCode, null);
    }

    public static void requestPermission(Activity activity, Permission permission) {
        requestPermission(activity, permission, getPermissionCode(permission), null);
    }

    public static void requestPermission(Activity activity, Permission permission, PermissionsCallBack callBack) {
        requestPermission(activity, permission, getPermissionCode(permission), callBack);
    }

    public static void requestPermission(Activity activity, Permission permission, int requestCode, PermissionsCallBack permissionsCallBack) {
        setPermissionsCallBack(permissionsCallBack);
        String[] permissions = {getPermission(permission)};
        if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])) {
            //用户拒绝，并且勾了不再提示，这时候只能跳转到设置页,统一处理，弹窗->引导去设置页
//            permissionsCallBack.noAsk();
//            return;
        }
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

//    public static void requestPermission(Activity activity, String[] permissions, int requestCode, PermissionsCallBack permissionsCallBack) {
//        setPermissionsCallBack(permissionsCallBack);
//        ActivityCompat.requestPermissions(activity, permissions, requestCode);
//    }

    public static boolean checkPermission(Activity activity, Permission permission) {
        String[] permissions = {getPermission(permission)};
        int permissionCheck = ContextCompat.checkSelfPermission(activity, permissions[0]);
        //shouldShowRequestPermissionRationale()。如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true
        //注：如果用户在过去拒绝了权限请求，并在权限请求系统对话框中选择了 Don’t ask again 选项，此方法将返回 false
        // 如果设备规范禁止应用具有该权限，此方法也会返回 false
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])) {
            //用户拒绝，并且勾了不再提示，这时候只能跳转到设置页,统一处理，弹窗->引导去设置页
            return false;
        }
        //如果应用具有此权限，方法将返回 PackageManager.PERMISSION_GRANTED
        //如果应用不具有此权限，方法将返回 PERMISSION_DENIED
        return false;
    }

    public static String getPermission(Permission permission) {
        switch (permission) {
            case PERMISSIONS_WRITE_EXTERNAL_STORAGE:
                return Manifest.permission.WRITE_EXTERNAL_STORAGE;

            case PERMISSIONS_READ_EXTERNAL_STORAGE:
                return Manifest.permission.READ_EXTERNAL_STORAGE;

            case PERMISSIONS_READ_PHONE_STATE:
                return Manifest.permission.READ_PHONE_STATE;

            case PERMISSIONS_CAMERA:
                return Manifest.permission.CAMERA;

            case PERMISSIONS_LOCATION:
                return Manifest.permission.ACCESS_FINE_LOCATION;

            case PERMISSIONS_CONTACTS:
                return Manifest.permission.READ_CONTACTS;
            default:
                return "";
        }
    }

    public static int getPermissionCode(Permission permission) {
        switch (permission) {
            case PERMISSIONS_WRITE_EXTERNAL_STORAGE:
                return PERMISSIONS_WRITE_EXTERNAL_STORAGE;

            case PERMISSIONS_READ_EXTERNAL_STORAGE:
                return PERMISSIONS_READ_EXTERNAL_STORAGE;

            case PERMISSIONS_READ_PHONE_STATE:
                return PERMISSIONS_READ_PHONE_STATE;

            case PERMISSIONS_CAMERA:
                return PERMISSIONS_CAMERA;

            case PERMISSIONS_LOCATION:
                return PERMISSIONS_LOCATION;

            case PERMISSIONS_CONTACTS:
                return PERMISSIONS_CONTACTS;
            default:
                return 0;
        }
    }

    public enum Permission {
        //读文件
        PERMISSIONS_WRITE_EXTERNAL_STORAGE()
        //写文件
        , PERMISSIONS_READ_EXTERNAL_STORAGE()
        //手机状态
        , PERMISSIONS_READ_PHONE_STATE()
        //相机
        , PERMISSIONS_CAMERA()
        //通讯录
        , PERMISSIONS_CONTACTS()
        //定位
        , PERMISSIONS_LOCATION();

        int permissions;

        Permission() {

        }

        Permission(int permissions) {
            this.permissions = permissions;
        }

        public int getPermissions() {
            return permissions;
        }
    }

    private static PermissionsCallBack mPermissionsCallBack;

    public static void setPermissionsCallBack(PermissionsCallBack permissionsCallBack) {
        mPermissionsCallBack = permissionsCallBack;
    }

    public static void permissionCallBack(Activity activity, int requestCode, String[] permissions, int[] grantResults) {
        if (mPermissionsCallBack != null) {
            int permissionCheck = ContextCompat.checkSelfPermission(activity, permissions[0]);
            //shouldShowRequestPermissionRationale()。如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true
            //注：如果用户在过去拒绝了权限请求，并在权限请求系统对话框中选择了 Don’t ask again 选项，此方法将返回 false
            //如果设备规范禁止应用具有该权限，此方法也会返回 false
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                mPermissionsCallBack.permissionsGranted(requestCode);
                return;
            }
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])) {
                //用户拒绝，并且勾了不再提示，这时候只能跳转到设置页,统一处理，弹窗->引导去设置页
                mPermissionsCallBack.noAsk(requestCode);
                return;
            }
            mPermissionsCallBack.permissionsDenied(requestCode);
        }
    }

    public interface PermissionsCallBack {
        void permissionsGranted(int requestCode);//允许

        void permissionsDenied(int requestCode);//拒绝

        void noAsk(int requestCode);//不再提示
    }
}
